package com.centling.dao.impl;

import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.centling.business.OfflinemeasureVO;
import com.centling.core.BaseDAO;
import com.centling.dao.IOfflinemeasureDAO;
import com.centling.util.LocalUtils;

@Repository
@Transactional
public class OfflinemeasureDAO extends BaseDAO implements IOfflinemeasureDAO{
	
	private static Logger log = Logger.getLogger(OfflinemeasureDAO.class);

	@Transactional
	public void save(OfflinemeasureVO cb, Integer fID, Integer fItemID) {
		log.info("save Offlinemeasure invoke...");
		Session session = this.sessionFactory.getCurrentSession();
		StringBuffer hql = new StringBuffer();
		hql.append("insert into t_appoint(");
		hql.append("FID,FBillNo,FBase,");
		if(LocalUtils.isNotBlank(cb.getF_appoint_name()))			hql.append("F_appoint_name,");
		if(LocalUtils.isNotBlank(cb.getF_appoint_type()))			hql.append("F_appoint_type,");
		if(LocalUtils.isNotBlank(cb.getF_mobile()))					hql.append("F_mobile,");
		if(LocalUtils.isNotBlank(cb.getfDate()))					hql.append("FDate,");
		if(LocalUtils.isNotBlank(cb.getF_appoint_time()))			hql.append("F_appoint_time,");
		if(LocalUtils.isNotBlank(cb.getF_appoint_address()))		hql.append("F_appoint_address,");
		if(LocalUtils.isNotBlank(cb.getF_service_address()))		hql.append("F_service_address,");
		if(LocalUtils.isNotBlank(cb.getF_measure_status()))			hql.append("F_measure_status");
		hql.append(") values(");

		hql.append(":FID,:FBillNo,:FBase,");
		if(LocalUtils.isNotBlank(cb.getF_appoint_name()))			hql.append(":F_appoint_name,");
		if(LocalUtils.isNotBlank(cb.getF_appoint_type()))			hql.append(":F_appoint_type,");
		if(LocalUtils.isNotBlank(cb.getF_mobile()))					hql.append(":F_mobile,");
		if(LocalUtils.isNotBlank(cb.getfDate()))					hql.append(":FDate,");
		if(LocalUtils.isNotBlank(cb.getF_appoint_time()))			hql.append(":F_appoint_time,");
		if(LocalUtils.isNotBlank(cb.getF_appoint_address()))		hql.append(":F_appoint_address,");
		if(LocalUtils.isNotBlank(cb.getF_service_address()))		hql.append(":F_service_address,");
		if(LocalUtils.isNotBlank(cb.getF_measure_status()))			hql.append(":F_measure_status");
		hql.append(");");
		
		log.info("hql: "+hql.toString());
		Query query = session.createSQLQuery(hql.toString());
		
		query.setParameter("FID", fID);
		query.setParameter("FBillNo", cb.getfBillNo());
		query.setParameter("FBase", fItemID);

		if(LocalUtils.isNotBlank(cb.getF_appoint_name()))			query.setParameter("F_appoint_name", cb.getF_appoint_name());
		if(LocalUtils.isNotBlank(cb.getF_appoint_type()))			query.setParameter("F_appoint_type", cb.getF_appoint_type());
		if(LocalUtils.isNotBlank(cb.getF_mobile()))					query.setParameter("F_mobile", cb.getF_mobile());
		if(LocalUtils.isNotBlank(cb.getfDate()))					query.setParameter("FDate", cb.getfDate());
		if(LocalUtils.isNotBlank(cb.getF_appoint_time()))			query.setParameter("F_appoint_time", cb.getF_appoint_time());
		if(LocalUtils.isNotBlank(cb.getF_appoint_address()))		query.setParameter("F_appoint_address", cb.getF_appoint_address());
		if(LocalUtils.isNotBlank(cb.getF_service_address()))		query.setParameter("F_service_address", cb.getF_service_address());
		if(LocalUtils.isNotBlank(cb.getF_measure_status()))			query.setParameter("F_measure_status", cb.getF_measure_status());
		
		query.executeUpdate();
		
		log.info("save Offlinemeasure end.");
	}
}
